﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Nucleo.Collections;


namespace EventMonitor.EventLogs
{
	public class WindowsEventLog
	{

		#region " Methods "

		/// <summary>
		/// Gets the individual event log entries for a specific log.
		/// </summary>
		/// <param name="logName">The name of the log to get entries for.</param>
		/// <returns>A collection of entries.</returns>
		public static IEnumerable<EventLogEntry> GetEventLogEntries(string logName)
		{
			EventLog log = GetEventLogs().Single(i => i.Log == logName);

			int max = ((EventMonitor.App)System.Windows.Application.Current).ResultFilterRecordCount;
			if (max <= 0) max = log.Entries.Count;

			return log.Entries.OfType<EventLogEntry>().OrderByDescending(i => i.TimeGenerated).Take(max);
		}

		/// <summary>
		/// Gets the individual event log entries for a specific log.
		/// </summary>
		/// <param name="log">The log to get entries for.</param>
		/// <returns>A collection of entries.</returns>
		public static IEnumerable<EventLogEntry> GetEventLogEntries(EventLog log)
		{
			return GetEventLogEntries(log.Log);
		}

		/// <summary>
		/// Gets a collection of event logs.
		/// </summary>
		/// <returns>A collection of event logs.</returns>
		public static IEnumerable<EventLog> GetEventLogs()
		{
			return EventLog.GetEventLogs();
		}

		#endregion
	}
}
